package com.zichen.pr.system.controller;

import com.zichen.pr.common.constant.Constants;
import com.zichen.pr.common.core.domain.result.Result;
import com.zichen.pr.common.core.redis.RedisCache;
import com.zichen.pr.util.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FastByteArrayOutputStream;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @program: zichen
 * @description: 验证码操作处理
 * @author: rzhang
 * @create: 2021-04-09 10:01
 */
@RestController
public class CaptchaController {

    @Autowired
    private RedisCache redisCache;

    /**
     * 生成验证码
     */
    @GetMapping("/captchaImage")
    public Result getCode(HttpServletResponse response) throws IOException {
        // 保存验证码信息
        String uuid = IdUtils.simpleUUID();
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;

        String capStr = null, code = null;
        BufferedImage image = null;

        redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);

        Result ajax = Result.success();
        ajax.put("uuid", uuid);
        return ajax;
    }
}
